/*this program produces the tabulations behind Figure 5 and the effect estimates and needed placebo
estimate distirbution for the permutaiton test for home owner vacancy rate outcome presented in Table 6*/

clear all

/*change the following directory to wherever you are storing the data*/
cd "D:\state immigration laws\RESTATREVISION\program files for RESTAT readme"
use homeownervacancyt.dta
/*dropping states with similar legislation from the donor pool*/
drop if statefip==28
drop if statefip==44
drop if statefip==45
drop if statefip==49


 
gen timenew=time
sort statefip timenew

** Declare panel dataset

tsset statefip timenew




** Initial synthetic cohort analysis for Arizona.

#delimit;
synth vacancyrate vacancyrate(1(1)4) vacancyrate(5(1)8) vacancyrate(9(1)10)
vacancyrate(1 5 9) vacancyrate(2 6 10) vacancyrate(3 7)
vacancyrate(4 8) metrodummy pu18 p1829 p3039 p4049 p5064 p65plus nonwhite hispanic foreign poor renter, 
trunit(4) counit(1 2 5 6 8 9 10 11 12 13 15 16 17 18 19 20 21 22 23 24 25 26 27 29 30 31 32 33 34 35 36 37 38 39 40 41 42 46 47 48 50 51 53 54 55 56)  
trperiod(11) fig;
#delimit cr
**the following code creates a variable with title "dif_TU" which contains the difference in the outcome variable for arizona minus synthetic arizona for each year from 1998 through 2009


mat diffs = e(Y_treated) - e(Y_synthetic)
mat treated =e(Y_treated)
mat colnames treated = treated
mat synthcontrol=e(Y_synthetic)
mat colnames synthcontrol = synthetic
mat colnames diffs = dif_TU
svmat diffs, names(col)
svmat treated, names(col)
svmat synthcontrol, names(col)
list treated synthetic in 1/20



*the following code estimaes the synthetic treatment effect for each state in the donor pool to generate the distribution of placebo estimate for the permutation test of the LAWA effect


local wonka "1 2 5 6 8 9 10 11 12 13 15 16 17 18 19 20 21 22 23 24 25 26 27 29 30 31 32 33 34 35 36 37 38 39 40 41 42 46 47 48 50 51 53 54 55 56"

	set more off
	foreach val of local wonka {
		display as text "`val'"
		quietly: synth vacancyrate vacancyrate(1(1)4) vacancyrate(5(1)8) vacancyrate(9(1)10) vacancyrate(1 5 9) vacancyrate(2 6 10) vacancyrate(3 7) vacancyrate(4 8)  metrodummy pu18 p1829 p3039 p4049 p5064 p65plus nonwhite hispanic foreign poor renter, trunit(`val') trperiod(11)
		* Calculations for the plotting the gap
			mat diffs = e(Y_treated) - e(Y_synthetic)
			mat colnames diffs = dif_`val'
			svmat diffs, names(col)
			}
			
			
			
			

*the following statement prodocues a figure showing the treated minus synth differences for Arizona (in heavy line) and all other states in the donor pool
gen timenewr=timenew-11 
twoway line dif_1 timenewr, lcolor(black) || line dif_2 timenewr, lcolor(black) || line dif_5 timenewr, lcolor(black) || line dif_6 timenewr, lcolor(black) || line dif_8 timenewr, lcolor(black) || line dif_9 timenewr, lcolor(black) || line dif_10 timenewr, lcolor(black) || line dif_11 timenewr, lcolor(black)|| line dif_12 timenewr, lcolor(black) || line dif_13 timenewr, lcolor(black) || line dif_15 timenewr, lcolor(black)|| line dif_16 timenewr, lcolor(black) || line dif_17 timenewr, lcolor(black) || line dif_18 timenewr, lcolor(black) || line dif_19 timenewr, lcolor(black) || line dif_20 timenewr, lcolor(black) || line dif_21 timenewr, lcolor(black) || line dif_22 timenewr, lcolor(black) || line dif_23 timenewr, lcolor(black) || line dif_24 timenewr, lcolor(black) || line dif_25 timenewr, lcolor(black) || line dif_26 timenewr, lcolor(black) || line dif_27 timenewr, lcolor(black) || line dif_29 timenewr, lcolor(black) || line dif_30 timenewr, lcolor(black) || line dif_31 timenewr, lcolor(black) || line dif_32 timenewr, lcolor(black) || line dif_33 timenewr, lcolor(black) || line dif_34 timenewr, lcolor(black) || line dif_35 timenewr, lcolor(black) ||line dif_36 timenewr, lcolor(black) || line dif_37 timenewr, lcolor(black) || line dif_38 timenewr, lcolor(black) || line dif_39 timenewr, lcolor(black) || line dif_40 timenewr, lcolor(black) || line dif_41 timenewr, lcolor(black) || line dif_42 timenewr, lcolor(black) || line dif_46 timenewr, lcolor(black) || line dif_47 timenewr, lcolor(black) || line dif_48 timenewr, lcolor(black) || line dif_50 timenewr, lcolor(black) ||  line dif_51 timenewr, lcolor(black) ||  line dif_53 timenewr, lcolor(black) ||  line dif_54 timenewr, lcolor(black) ||  line dif_55 timenewr, lcolor(black) ||  line dif_56 timenewr, lcolor(black) || line dif_TU timenewr, lcolor(red)lwidth(vthick) || ,legend(off) ytitle("Difference treatment minuse synthetic control") xtitle("Quarter Relative to Intervention Quarter (2007Q3)") yline(0, lstyle(foreground) lpattern(dash)) xline(0)

** the code that follows uses the vectors of treated minus control difference to generate the key statistics for the difference-in-differnece estimates and the permutation test 

*restric the data set to obervations contaning the difference vectors
keep if dif_1< .

/*create 12 by 47 matrix of differences vectors for each state, take the transpose, and create variables with each observation a state and generating variables year1 through year12 
corresponding to 1998 through 2009 treated minus synthetic comparison differences.  Note, Arizona is last observation in data set (row 47)*/


mkmat dif_1 dif_2 dif_5 dif_6 dif_8 dif_9 dif_10 dif_11 dif_12 dif_13 dif_15 dif_16 dif_17 dif_18 dif_19 dif_20 dif_21 dif_22 dif_23 dif_24 dif_25 dif_26 dif_27 dif_29 dif_30 dif_31 dif_32 dif_33 dif_34 dif_35 dif_36 dif_37 dif_38 dif_39 dif_40 dif_41 dif_42 dif_46 dif_47 dif_48 dif_50 dif_51 dif_53 dif_54 dif_55 dif_56 dif_TU, matrix(diffs) rownames(timenew)
mat diffs2=diffs'
svmat diffs2, names(timenew)
 

*following code generates mean pre-intervention difference between treated and synth
gen premean=(timenew1+timenew2+timenew3+timenew4+timenew5+timenew6+timenew7+timenew8+timenew9+timenew10)/10
*following code generates mean pre-intervention difference between treated and synth
gen postmean=(timenew13+timenew14+timenew15+timenew16+timenew17+timenew18+timenew19+timenew20)/8
*the following tabulates the pre-post change in means for all states
gen diffmean=postmean-premean

*following code lists teh values for Arizona
list premean postmean diffmean in 47
/*the code that follows generates the output needed to perform the permutation inference tests.  We first list the pre and post means
for Arizona (observatin 47).  We then sort each diff-in-diff estimate and print the distribution.  Arizona's relative position in the distribution is needed 
to calculate the p-value in Table 6*/
sort diffmean
list diffmean

